package com.yannan.lock.web.controller;

import com.yannan.lock.core.constant.CoreConstant;
import com.yannan.lock.core.controller.BaseController;
import com.yannan.lock.core.vo.ResultResponse;
import com.yannan.lock.web.dto.*;
import com.yannan.lock.web.pojo.*;
import com.yannan.lock.web.service.InstallationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * PositionInformationController
 * 安装端位置信息相关
 *
 * @author wu
 * @create 2016-10-25 13:17
 */
@Controller
public class PositionInformationController extends BaseController {
    /** Field installationService ... */
    @Autowired
    private InstallationService installationService;

    /**
     * Method description
     * 通过手机号查询对应小区id和位置名字
     *
     * @param requests
     *
     * @return
     */
    @RequestMapping("/getCommunityList")
    @ResponseBody
    public ResultResponse getCommunityList(HttpServletRequest requests) {
        ResultResponse res = new ResultResponse();

        try {
            String userId = requests.getParameter("userId");

            if (StringUtils.isEmpty(userId)) {
                res.setMessage("需要用户编号(userId)");
                res.setRetCode(CoreConstant.RET_CODE_FAILED);

                return res;
            }

            List<CommunityRspDTO> list = installationService.getCommunityListByUserId(userId);
            if(list.size()==0){
                res.setRetCode(CoreConstant.RET_CODE_FAILED);
                res.setMessage("没有对应小区");
                return res;
            }
            res.setRetCode(CoreConstant.RET_CODE_SUCCESS);
            res.setMessage("获取成功。");
            res.setData(list);

            return res;

        } catch (Exception e) {
            log.error(e.getMessage(),e);
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("查询错误");

            return res;
        }
    }


    /**
     *  根据小区ID获取楼列表
     * @param request
     * @return
     */
    @RequestMapping("/getBuildingList")
    @ResponseBody
    public ResultResponse getBuildingInfo(HttpServletRequest request){
        ResultResponse res = new ResultResponse();
        String communityId = request.getParameter("communityId");
        if(StringUtils.isEmpty(communityId)){
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("小区编号(communityId)不能为空。");
            return  res;
        }
        String userId = request.getParameter("userId");
        if(StringUtils.isEmpty(userId)){
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("用户编号(userId)不能为空。");
            return  res;
        }
        try{
            InstallationuserRespon ir=new InstallationuserRespon();
            ir.setInstallationuserid(userId);
            ir.setNeighbourhoodsId(communityId);
            List<SelectGateWayByPlot> list = installationService.getBuildingInfoByCommunityId(ir);
            if(list.size()==0){
                res.setRetCode(CoreConstant.RET_CODE_FAILED);
                res.setMessage("没有对应楼号");
                return res;
            }
            res.setMessage("查询成功！");
            res.setRetCode(CoreConstant.RET_CODE_SUCCESS);
            res.setData(list);
            return res;

        }catch (Exception e){
            e.printStackTrace();
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("查询错误！"+e.getMessage());
            return res;
        }
    }

    /**
     * 根据楼的ID获取单元列表
     * @param request
     * @return
     */
    @RequestMapping("/getUnitList")
    @ResponseBody
    public ResultResponse getUnitList(HttpServletRequest request){
        ResultResponse res = new ResultResponse();
        String buildingId = request.getParameter("buildingId");
        if(StringUtils.isEmpty(buildingId)){
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("楼层编号(buildingId)不能为空。");
            return  res;
        }
        String userId = request.getParameter("userId");
        if(StringUtils.isEmpty(userId)){
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("用户编号(userId)不能为空。");
            return  res;
        }
        try{
            InstallationuserRespon ir=new InstallationuserRespon();
            ir.setInstallationuserid(userId);
            ir.setUildingId(buildingId);
            List<UnitInfoRspDTO> list = installationService.getUnitListBybuildingId(ir);
            if(list.size()==0){
                res.setRetCode(CoreConstant.RET_CODE_FAILED);
                res.setMessage("没有对应单元");
                return res;
            }
            res.setMessage("查询成功！");
            res.setRetCode(CoreConstant.RET_CODE_SUCCESS);
            res.setData(list);
            return res;

        }catch (Exception e){
            e.printStackTrace();
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("查询错误！"+e.getMessage());
            return res;
        }

    }

    /**
     * 根据单元和楼层获取网关列表     调用位置：状态查询和设备扫码
     * @param request
     * @return
     */

    @RequestMapping("/getGatewayList")
    @ResponseBody
    public ResultResponse getGatewayList(HttpServletRequest request){
        ResultResponse res = new ResultResponse();
        String unitId = request.getParameter("unitId");
        if(StringUtils.isEmpty(unitId)){
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("单元编号(unitId)不能为空。");
            return  res;
        }
        String requestType = request.getParameter("requestType");
        if(StringUtils.isEmpty(requestType)){
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("请求类型(requestType)不能为空。");
            return  res;
        }
        String floorNum = request.getParameter("floorNum");
        if(StringUtils.isEmpty(floorNum)){
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("楼层(floorNum)不能为空。");
            return  res;
        }
        try{
            PositionInfo p=new PositionInfo();
            p.setUnitId(unitId);
            p.setFloorId(floorNum);
            List<GateWatRspDTO> list=installationService.getGatewayListByunitId(p,requestType);
            if(list.size()==0){
                res.setRetCode(CoreConstant.RET_CODE_FAILED);
                res.setMessage("获取错误！没有对应网关");
                return res;
            }
            res.setMessage("获取成功！");
            res.setRetCode(CoreConstant.RET_CODE_SUCCESS);
            res.setData(list);
            return res;
        }catch (Exception e){
            e.printStackTrace();
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("获取错误！"+e.getMessage());
            return res;
        }

    }

    /**
     * 根据单元和楼层获取门锁列表     调用位置：状态查询和设备扫码
     * @param request
     * @return
     */
    @RequestMapping("/getLockList")
    @ResponseBody
    public ResultResponse getLockList(HttpServletRequest request){
        ResultResponse res = new ResultResponse();
        String unitId = request.getParameter("unitId");
        if(StringUtils.isEmpty(unitId)){
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("单元编号(unitId)不能为空。");
            return  res;
        }
        String requestType = request.getParameter("requestType");
        if(StringUtils.isEmpty(requestType)){
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("请求类型(requestType)不能为空。");
            return  res;
        }
        String floorNum = request.getParameter("floorNum");
        if(StringUtils.isEmpty(floorNum)){
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("楼层(floorNum)不能为空。");
            return  res;
        }
        try{
            PositionInfo p=new PositionInfo();
            p.setUnitId(unitId);
            p.setFloorId(floorNum);
            List<LockInfoRspDTO> list=installationService.getLockListByunitId(p,requestType);
            if(list.size()==0){
                res.setRetCode(CoreConstant.RET_CODE_FAILED);
                res.setMessage("获取错误！没有对应锁");
                return res;
            }
            res.setMessage("获取成功！");
            res.setRetCode(CoreConstant.RET_CODE_SUCCESS);
            res.setData(list);
            return res;
        }catch (Exception e){
            e.printStackTrace();
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("获取错误！"+e.getMessage());
            return res;
        }
    }

    /**
     * 扫码绑定
     * @param request
     * @return
     */
    @RequestMapping("/scanAddBinding")
    @ResponseBody
    public ResultResponse scanAddBinding(HttpServletRequest request){
        ResultResponse res = new ResultResponse();
        String deviceId = request.getParameter("deviceId");
        if(StringUtils.isEmpty(deviceId)){
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("设备Id(deviceId)不能为空。");
            return  res;
        }
        String deviceAddress = request.getParameter("deviceAddress");
        if(StringUtils.isEmpty(deviceAddress)){
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("设备地址(deviceAddress)不能为空。");
            return  res;
        }
        String isLock = request.getParameter("isLock");
        if(StringUtils.isEmpty(isLock)){
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("请选择是否是门锁(isLock)。");
            return  res;
        }
        try{
            String isNo=installationService.scanAddBinding(deviceId,deviceAddress,isLock);
            res.setMessage("获取成功！");
            res.setRetCode(CoreConstant.RET_CODE_SUCCESS);
            res.setData(new ReturnValueRspDTO(isNo));
            return res;
        }catch (Exception e){
            e.printStackTrace();
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("获取错误！"+e.getMessage());
            res.setData(new ReturnValueRspDTO("0"));
            return res;
        }
    }

    /**
     * 根据网关ID获取网关绑定的门锁
     * @param request
     * @return
     */
    @RequestMapping("/getBindedLockList")
    @ResponseBody
    public ResultResponse getBindedLockList(HttpServletRequest request){
        ResultResponse res = new ResultResponse();
        String gatewayId = request.getParameter("gatewayId");
        if(StringUtils.isEmpty(gatewayId)){
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("网关序列号(gatewayId)不能为空。");
            return  res;
        }
        try{
            List<LockidANDnameDTO> list=installationService.getBindedLockListBygatewayId(gatewayId);

            res.setMessage("获取成功！");
            res.setRetCode(CoreConstant.RET_CODE_SUCCESS);
            res.setData(list);
            return res;
        }catch (Exception e){
            e.printStackTrace();
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("获取错误！"+e.getMessage());
            return res;
        }

    }

    /**
     * 调整绑定关系
     * @param request
     * @return
     */

    @RequestMapping("/bindedState")
    @ResponseBody
    public ResultResponse bindedState(HttpServletRequest request){
        ResultResponse res = new ResultResponse();
        String gatewayId = request.getParameter("gatewayId");
        if(StringUtils.isEmpty(gatewayId)){
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("网关序列号(gatewayId)不能为空。");
            return  res;
        }
        String lockId = request.getParameter("lockId");
        if(StringUtils.isEmpty(lockId)){
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("门锁Id(lockId)不能为空。");
            return  res;
        }
        String isBinded = request.getParameter("isBinded");
        if(StringUtils.isEmpty(isBinded)){
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("请选择是否绑定(isBinded)。");
            return  res;
        }
        GateLockRealionInfoKey gl=new GateLockRealionInfoKey();
        gl.setLockId(lockId);
        gl.setGateWayId(gatewayId);
        try{
            String isNo=installationService.bindedState(gl,isBinded);
            if (isNo.equals("1")){
                res.setMessage("获取成功！");
                res.setRetCode(CoreConstant.RET_CODE_SUCCESS);
                res.setData(new ReturnValueRspDTO(isNo));
            }else {
                res.setRetCode(CoreConstant.RET_CODE_FAILED);
                res.setMessage("获取错误！");
                res.setData( new ReturnValueRspDTO("0"));
            }
            return res;
        }catch (Exception e){
            e.printStackTrace();
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("获取错误！");
            res.setData( new ReturnValueRspDTO("0"));
            return res;
        }
    }

    /**
     * 获取安装图纸列表
     * @param request
     * @return
     */

    @RequestMapping("/getBuildingImageList")
    @ResponseBody
    public ResultResponse getBuildingImageList(HttpServletRequest request){
        ResultResponse res = new ResultResponse();
        String communityId = request.getParameter("communityId");
        if(StringUtils.isEmpty(communityId)){
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("小区编号(communityId)不能为空。");
            return  res;
        }
        String buildingId = request.getParameter("buildingId");
        if(StringUtils.isEmpty(buildingId)){
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("楼的编号(buildingId)不能为空。");
            return  res;
        }
        try{
            EnclosureInfo ei=new EnclosureInfo();
            ei.setuildingId(buildingId);
            ei.setNeighbourhoodsId(communityId);
            List<ImageRspDTO> list=installationService.getBuildingImageListByEnclosureInfo(ei);
            if(list.size()==0){
                res.setRetCode(CoreConstant.RET_CODE_FAILED);
                res.setMessage("获取失败！没有对应单元图纸");
                return res;
            }
            res.setMessage("获取成功！");
            res.setRetCode(CoreConstant.RET_CODE_SUCCESS);
            res.setData(list);
            return res;
        }catch (Exception e){
            e.printStackTrace();
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("获取错误！"+e.getMessage());
            return res;
        }
    }


}